<template>
    <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" center>
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="100px">
            <el-form-item label="权限名称" prop="name">
                <el-input v-model="dataForm.name" placeholder="权限名称" />
            </el-form-item>
            <el-form-item label="权限路径" prop="url">
                <el-input v-model="dataForm.url" placeholder="权限路径" />
            </el-form-item>
            <el-form-item label="菜单图标" prop="icon">
                <el-input v-model="dataForm.icon" placeholder="菜单图标"></el-input>
            </el-form-item>
            <el-form-item label="类型" prop="type">
                <el-radio-group v-model="dataForm.type" :disabled="operationType == 2">
                    <el-radio :label="0">目录</el-radio>
                    <el-radio :label="1">菜单</el-radio>
                    <el-radio :label="2">按钮</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-radio-group v-model="dataForm.status">
                    <el-radio :label="1">启用</el-radio>
                    <el-radio :label="0">禁用</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="是否展示" prop="showFlag">
                <el-radio-group v-model="dataForm.showFlag">
                    <el-radio :label="0">不展示</el-radio>
                    <el-radio :label="1">展示</el-radio>
                </el-radio-group>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>

export default {
    data() {
        return {
            visible: false,
            dataForm: {
                id: null,
                parentId: null,
                name: '',
                url: '',
                type: null,
                status: null,
                icon: null,
                showFlag: null,
                orderNum: null
            },
            dataRule: {
                parentId: [
                    { required: true, message: '父菜单ID不能为空', trigger: 'blur' }
                ],
                name: [
                    { required: true, message: '权限名称不能为空', trigger: 'blur' }
                ],
                type: [
                    { required: true, message: '类型不能为空', trigger: 'blur' }
                ]
            },
            operationType: null
        }
    },
    methods: {
        /**
         * 初始化
         * @param {操作类型} operationType 0.新增一级权限 1.添加下级权限 2.修改权限
         * @param {表单数据} data 
         */
        init(operationType, data) {
            this.dataForm = {};
            this.operationType = operationType;
            this.visible = true
            if (operationType == 2 && data) {
                this.dataForm = data;
            }
            if (operationType == 1) {
                this.dataForm.parentId = data.id;
            }
        },
        // 表单提交
        dataFormSubmit() {
            this.$refs['dataForm'].validate((valid) => {
                if (!valid) {
                    return;
                }

                // 新增一级权限或者添加下级权限，调用新增接口
                if (this.operationType == 0 || this.operationType == 1) {
                    this.addPermission();
                } else {
                    this.updatePermission();
                }
            })
        },
        addPermission() {
            this.$http({
                url: `/pms/admin/permission/addPermission`,
                method: 'post',
                data: {
                    parentId: this.dataForm.parentId,
                    name: this.dataForm.name,
                    url: this.dataForm.url,
                    type: this.dataForm.type,
                    status: this.dataForm.status,
                    showFlag: this.dataForm.showFlag,
                    icon: this.dataForm.icon
                }
            }).then((response) => {
                if (response && response.code == "0") {
                    this.visible = false
                    this.$emit('refreshDataList')
                    this.$message({
                        message: '操作成功',
                        type: 'success',
                        duration: 1500
                    })
                } else {
                    this.$message.error(response.message)
                }
            })
        },
        updatePermission() {
            this.$http({
                url: `/pms/admin/permission/updatePermission`,
                method: 'post',
                data: {
                    id: this.dataForm.id,
                    name: this.dataForm.name,
                    url: this.dataForm.url,
                    status: this.dataForm.status,
                    showFlag: this.dataForm.showFlag,
                    icon: this.dataForm.icon
                }
            }).then((response) => {
                if (response && response.code == "0") {
                    this.visible = false
                    this.$emit('refreshDataList')
                    this.$message({
                        message: '操作成功',
                        type: 'success',
                        duration: 1500
                    })
                } else {
                    this.$message.error(response.message)
                }
            })
        }
    }
}
</script>
